<?php


namespace App\Services;

class EsService
{
    protected $index = 'article_index';

    protected $type  = 'article_type';


    /**
     * 搜索数据
     */
    public function search($type,$conditions = [])
    {
        $params = [
            'index' => $this->index,
            'type'  => $this->type,
        ];

        switch ($type){
            case 'page':
                $c = array_merge($params,$conditions);
                return  app('es')->search($c);

            case 'all':
                return  app('es')->search($params);
            case 'q':
            case 'sort':
                $params['body'] = $conditions;
                return  app('es')->search($params);

        }
    }

    //  创建索引
    public function createIndex()
    {
        $params = [
            'index' =>  $this->index,
            'body'  =>  [
                'settings' => [
                    'number_of_shards' => 1,
                    'number_of_replicas' => 2
                ],
                'mappings'  =>  [
                    $this->type   =>  [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            'author'    =>  [
                                'type'  =>  'text',
                            ],
                            'body'    =>  [
                                'type'  =>  'text',
                            ],
                            'countries'    =>  [
                                'type'  =>  'keyword',
                            ],
                            'desc'    =>  [
                                'type'  =>  'text',
                            ],
                            'created_at' => [
                                'type' => 'date',
                                "format" => "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy/MM/dd||yyyy/MM||strict_date_optional_time||epoch_millis"
                            ],
                        ]
                    ]
                ]
            ]
        ];

        return app('es')->indices()->create($params);
    }

    /**
     * 删除索引
     */
    public function deleteIndex($index = '')
    {
        $deleteParams = [
            'index' => $index == '' ? $this->index : $index,
        ];

        return app('es')->indices()->delete($deleteParams);
    }


    /**
     *添加数据到es
     * @param $data  // 数据
     * @param array $field //   需要索引的字段
     */
    public function add($data,$field = [])
    {

        foreach ($data as $k => $v){

            $temp = [];
            foreach ($field as $kk => $vv){
                $temp[$vv] = $v->$vv;
            }

            $params['body'][] = [
                'index' => [
                    '_index'    =>  $this->index,
                    '_type'     =>  $this->type
                ]
            ];
            $params['body'][] = $temp;
        }

        $responses = app('es')->bulk($params);

        dd($responses);
    }
}